package com.allwees.bs.module.mms.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.bs.module.core.dynamic.annotation.DynamicInject;
import com.allwees.bs.module.core.rest.BoCrudApi;
import com.allwees.bs.module.mms.encrypt.ApplyEmailEncryptor;
import com.allwees.bs.module.mms.entity.RetailerApply;
import com.allwees.bs.module.mms.req.RetailerApplyAuditReq;
import com.allwees.bs.module.mms.req.query.RetailerApplyQueryReq;
import com.allwees.bs.module.mms.req.query.RetailerQueryApplyProgressReq;
import com.allwees.bs.module.mms.req.register.RetailerRegisterAccountReq;
import com.allwees.bs.module.mms.req.register.RetailerRegisterBaseReq;
import com.allwees.bs.module.mms.req.register.RetailerRegisterResendNewEmailReq;
import com.allwees.bs.module.mms.req.register.RetailerRegisterResendReq;
import com.allwees.bs.module.mms.req.register.RetailerRegisterVerifyReq;
import com.allwees.bs.module.mms.service.RetailerApplyService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author yangylsky
 * @since 2019-12-03
 */
@RestController
@RequestMapping("/backstage/api/mms/retailer_apply")
public class RetailerApplyApi extends BoCrudApi<RetailerApply, RetailerApplyService> {

    @DynamicInject(type = DynamicInject.Type.Category,key = "categoryNo")
    @GetMapping("/page")
    public R page(Page<RetailerApply> page, RetailerApplyQueryReq req) {
        return super.page(page, req);
    }

    /**
     * 提交合作申请并发送验证邮件
     * @param req
     * @return
     */
    @PostMapping("/register/verify_info")
    public R registerBase(@RequestBody RetailerRegisterVerifyReq req) {
        RetailerApply apply = boService.registerVerify(req);//手机不做校验
        return R.ok(apply);
    }

    /**
     * 根据邮箱的验证连接进行验证
     * @param d
     * @return
     */
    @GetMapping("/register/email_verify")
    public ModelAndView registerEmailVerify(String d) {
        boService.registerEmailVerify(ApplyEmailEncryptor.decrypt(d));
        return new ModelAndView("register_sucess_b");
    }

    /**
     * 校验邮箱是否验证
     * @param uuid
     * @return
     */
    @GetMapping("/register/{uuid}/is_email_verified")
    public R registerIsEmailVerified(@PathVariable String uuid) {
        boolean verified = boService.registerIsEmailVerified(uuid);
        return R.ok(verified);
    }

    /**
     * 填写账户(商铺)信息
     * @param req
     * @return
     */
    @PostMapping("/register/base")
    public R registerBase(@RequestBody RetailerRegisterBaseReq req) {
        RetailerApply apply = boService.registerBase(req);//添加类目
        return R.ok(apply);
    }

    /**
     * 合作申请提交审核
     * @param req
     * @return
     */
    @PostMapping("/register/account")
    public R registerAccount(@RequestBody RetailerRegisterAccountReq req) {
        RetailerApply apply = boService.registerAccount(req);//添加商户类型
        return R.ok(apply);
    }


    @PostMapping("/register/resend")
    public R registerBase(@RequestBody RetailerRegisterResendReq req) {
        boService.registerResend(req);
        return R.ok();
    }

    @PostMapping("/register/resend_new_email")
    public R registerResendNewEmail(@RequestBody RetailerRegisterResendNewEmailReq req) {
        boService.registerResend(req);
        return R.ok();
    }


    @PostMapping("/first_audit")
    public R firstAudit(@RequestBody RetailerApplyAuditReq req) {
        boService.audit(req);
        return R.ok();
    }

    @PostMapping("/final_audit")
    public R finalAudit(@RequestBody RetailerApplyAuditReq req) {
        boService.audit(req);//保存类目和商户类型至mms_store
        return R.ok();
    }

    @PostMapping("/find_audit_status")
    public R findAuditStatus(@RequestBody RetailerQueryApplyProgressReq req) {
        String statusText = boService.findAuditStatus(req.getMobile());
        return R.ok(statusText);
    }
}
